package com.tbit.main.pojo.maintain_performance.config;

import com.tbit.main.constant.enums.PerformanceBizType;
import com.tbit.main.pojo.PerformanceRuleGroup;
import com.tbit.main.pojo.maintain_performance.BizPerformanceConfig;
import com.tbit.main.pojo.maintain_performance.BizRuleGroup;
import com.tbit.main.pojo.maintain_performance.group.*;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;

/**
 * 挪车绩效配置的业务对象
 *
 * @author <a href="mailto:zheng.zebiao@tbit.com.cn">zb</a>
 * @since 2025/2/21
 */
@Data
public class MovePerformanceConfig implements BizPerformanceConfig {
    /**
     * 区域id
     */
    private Integer accountId;

    /**
     * 基础绩效
     */
    private MoveBasicPerformanceGroup basic;
    /**
     * 产生订单时段绩效
     */
    //private IntervalBasedGroup intervalBased;
    /**
     * 产生订单时效绩效
     */
    //private ValidityBasedGroup validityBased;
    /**
     * 产生订单时段与时效组合绩效
     */
    private IntervalAndValidityBasedGroup intervalAndValidityBased;
    /**
     * 异常车辆处理绩效
     */
    private AbnormalSolvedGroup abnormalSolved;
    /**
     * 挪车距离绩效
     */
    private MoveDistanceBasedGroup distanceBased;

    public static MovePerformanceConfig fromRaw(List<PerformanceRuleGroup> groups, Integer accountId) {
        MovePerformanceConfig config = new MovePerformanceConfig();
        config.setAccountId(accountId);
        for (PerformanceRuleGroup group : groups) {
            switch (group.getRuleType()) {
                case BASIC:
                    config.setBasic(new MoveBasicPerformanceGroup());
                    config.getBasic().initFromRaw(group);
                    break;
                case INTERVAL_AND_VALIDITY:
                    config.intervalAndValidityBased = new IntervalAndValidityBasedGroup();
                    config.intervalAndValidityBased.initFromRaw(group);
                    break;
                case MACHINE_ABNORMAL:
                    config.setAbnormalSolved(new AbnormalSolvedGroup());
                    config.getAbnormalSolved().initFromRaw(group);
                    break;
                case DISTANCE:
                    config.setDistanceBased(new MoveDistanceBasedGroup());
                    config.getDistanceBased().initFromRaw(group);
                    break;
            }
        }
        return config;
    }

    @Override
    public List<PerformanceRuleGroup> toRaw() {
        BizRuleGroup[] groups = new BizRuleGroup[]{
                basic,
                intervalAndValidityBased,
                abnormalSolved,
                distanceBased,
        };

        List<PerformanceRuleGroup> rawGroups = new ArrayList<>(5);
        for (BizRuleGroup group : groups) {
            if (group != null) {
                rawGroups.add(group.toRaw(PerformanceBizType.MOVE, accountId));
            }
        }
        return rawGroups;
    }
}
